package com.library.cxq.dao.impl;

import com.library.cx.into.Libary;
import com.library.cxq.dao.ReaderDao;
import com.library.cxq.util.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReaderDaoImpl extends BaseDao implements ReaderDao {
    /**
     * 注册
     * @param reaId
     * @param idCard
     * @param pwd
     * @param name
     * @param age
     * @param phone
     * @param price
     * @return
     */
    @Override
    public Integer register(Integer reaId, String idCard, String pwd, String name, Integer age, String phone, double price) {
        String sql="INSERT INTO `reader`(`reaId`,`idCard`,`pwd`,`name`,`age`,`phone`,`price`)VALUES(?,?,?,?,?,?,?)";
        return executeUpdate(sql,reaId,idCard,pwd,name,age,phone,price);
    }
    ResultSet rs=null;
    @Override
    public Integer login(Integer reaId, String pwd) {
        String sql="SELECT COUNT(*) FROM `reader` WHERE `reaId`=? AND `pwd`=?";
         rs = executeQuery(sql, reaId, pwd);
        Integer i=null;
                try {
                    while (rs.next()) {
                        i=rs.getInt(1);
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
        return i;
    }

    /**
     * 核实信息
     * @param phone 电话
     * @param cardId 身份证
     * @return
     */
    @Override
    public Integer verification(String phone, String cardId) {
        String sql="select count(1) from  `reader` where reader.idCard=? and  reader.phone=?";
        rs=executeQuery(sql,cardId,phone);
        Integer count=0;
        try {
            while (rs.next()){
                count=rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    /**
     * 找回密码 修改密码
     * @param cardId
     */
    @Override
    public void updatePwd(String cardId,String pwd) {
        String sql="UPDATE reader set reader.pwd=? where reader.idCard=?";
        executeUpdate(sql,pwd,cardId);
    }

    /**
     * 修改密码
     * @param id
     * @param pwd
     */
    @Override
    public void updatePwd(Integer id, String pwd) {
        String sql="UPDATE reader set reader.pwd=? where reader.reaId=?";
        executeUpdate(sql,pwd,id);
    }

    /**
     * 书籍查询
     */
    public  List<Libary> selectBook(String name,String zuo){
            String sql = "SELECT bookId,bookName,type,author,sum,price,location,source,jdate FROM `book`";
            if(name!=null&&zuo!=null){
                sql+=" where bookName like concat('%',?,'%') and author like concat('%',?,'%')";
                rs=executeQuery(sql,name,zuo);
            }else if (name!=null) {
                sql+=" where bookName like concat('%',?,'%') ";
                rs=executeQuery(sql,name);
            }else if(zuo!=null){
                sql+=" where author like concat('%',?,'%')";
                rs=executeQuery(sql,zuo);
            }else {
                rs = executeQuery(sql);
            }

            //根据书名查询
            List<Libary> list = new ArrayList<>();
            try {
                while (rs.next()) {
                    Integer id = rs.getInt(1);
                    String bookname = rs.getString(2);
                    String type = rs.getString(3);
                    String author = rs.getString(4);
                    Integer sum = rs.getInt(5);
                    Integer price = rs.getInt(6);
                    String location = rs.getString(7);
                    String source = rs.getString(8);
                    String jdate = rs.getString(9);
                    Libary libary=new Libary(id,bookname,type,author,sum,price,location,source,jdate);
                    list.add(libary);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return list;
        }

}
